package poker;

import java.util.Collection;
import java.util.List;
import java.util.Vector;

import javax.servlet.ServletContext;
import java.io.*;
import org.directwebremoting.*;
import org.directwebremoting.proxy.ScriptProxy;

public class Chat {

   private static List<String> chatContent = new Vector<String>();

   public void postMessage(String message, String userName) {
         StringBuffer sb = new StringBuffer();
         sb.append("<br/>");
         sb.append(userName);
         sb.append(":");
         sb.append("<br/>");
         sb.append(message);
         String newMessage=sb.toString();
         //TODO meetod, mis poolitab stringi sobilikeks juppideks
         chatContent.add(newMessage);
         postNewMessage(newMessage);
         writeToFile(newMessage);
   }

   public List<String> getChatContent() {
      return chatContent;
   }

   private ScriptProxy getScriptProxyForSessions() {
      WebContext webContext = WebContextFactory.get();
      ServletContext servletContext = webContext.getServletContext();
      ServerContext serverContext = ServerContextFactory.get(servletContext);
      webContext.getScriptSessionsByPage("");
      String contextPath = servletContext.getContextPath();
      if (contextPath != null) {
         Collection<ScriptSession> sessions = serverContext
               .getScriptSessionsByPage(contextPath + "/gameRoom.html");
         ScriptProxy proxy = new ScriptProxy(sessions);
         return proxy;
      }
      return null;
   }

   public void postNewMessage(String newMessage) {
      ScriptProxy proxy = getScriptProxyForSessions();
      if (proxy != null) {
         proxy.addFunctionCall("newMessage",newMessage);
      }
   }
   
   public void botChatToHTML(){
	   String[] botchat = {
		   "Lucky bastard",
		   "Bad cards",
		   "I thin I'am the winner",
		   "Learn how to play!",
		   "Full house, naiss",
		   "Oh god why?",
		   "Who farted",
		   "Hey!",
		   "Cheater!",
		   "Batman has stinky feet",
		   "ZZZZZZ..."};
	   
	   String[] players = {
			"Joker",
			"Alfred",
			"Robin",
			"Mr. Freeze",
			"Riddler",
			"Egghead",
			"Penguin",
			"Falce-Face",
	   };
	   
	   int when = (int) Math.round(Math.random()*10);
	   int who = (int) Math.round(Math.random()*7);
	   int what = (int) Math.round(Math.random()*10);
	   if (when > 5){
		   try{
		   Thread.sleep(1000);
		   postMessage(botchat[what], players[who]);
		   writeToFile(botchat[what]);
		   }
		   catch (InterruptedException x){
			   System.out.println("in run() - interrupted while sleeping");
			   return;
		   }
		   }
	   }
   
   //Write input text to file
   
   public void writeToFile(String text){
   	try {
   		FileWriter outFile = new FileWriter("chat.txt");
   	    PrintWriter out = new PrintWriter(outFile);   	    
   	    out.println(text);
   	    out.close();	
   	} 
   	catch (IOException e){
   		e.printStackTrace();
   	}
   }
}